// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.objects;

import java.io.IOException;
import java.nio.ByteBuffer;
import zombie.characters.IsoPlayer;
import zombie.characters.skills.PerkFactory;
import zombie.inventory.InventoryItem;
import zombie.inventory.InventoryItemFactory;
import zombie.inventory.ItemContainer;
import zombie.inventory.types.DrainableComboItem;
import zombie.iso.IsoCell;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoLightSource;
import zombie.iso.IsoObject;
import zombie.iso.IsoWorld;
import zombie.iso.sprite.IsoSpriteManager;
import zombie.network.GameClient;
import zombie.network.GameServer;

public class BSFurnace extends IsoObject {
    public float heat = 0.0F;
    public float heatDecrease = 0.005F;
    public float heatIncrease = 0.001F;
    public float fuelAmount = 0.0F;
    public float fuelDecrease = 0.001F;
    public boolean fireStarted = false;
    private IsoLightSource LightSource;
    public String sSprite;
    public String sLitSprite;

    public BSFurnace(IsoCell cell) {
        super(cell);
    }

    public BSFurnace(IsoCell cell, IsoGridSquare sq, String sprite, String litSprite) {
        super(cell, sq, IsoSpriteManager.instance.getSprite(sprite));
        this.sSprite = sprite;
        this.sLitSprite = litSprite;
        this.sprite = IsoSpriteManager.instance.getSprite(sprite);
        this.square = sq;
        this.container = new ItemContainer();
        this.container.setType("stonefurnace");
        this.container.setParent(this);
        sq.AddSpecialObject(this);
    }

    @Override
    public void update() {
        this.updateHeat();
        if (!GameClient.bClient) {
            DrainableComboItem drainableComboItem = null;
            InventoryItem inventoryItem0 = null;

            for (int int0 = 0; int0 < this.getContainer().getItems().size(); int0++) {
                InventoryItem inventoryItem1 = (InventoryItem)this.getContainer().getItems().get(int0);
                if (inventoryItem1.getType().equals("IronIngot") && ((DrainableComboItem)inventoryItem1).getUsedDelta() < 1.0F) {
                    drainableComboItem = (DrainableComboItem)inventoryItem1;
                }

                if (inventoryItem1.getMetalValue() > 0.0F) {
                    if (this.getHeat() > 15.0F) {
                        if (inventoryItem1.getItemHeat() < 2.0F) {
                            inventoryItem1.setItemHeat(
                                inventoryItem1.getItemHeat() + 0.001F * (this.getHeat() / 100.0F) * zombie.GameTime.instance.getMultiplier()
                            );
                        } else {
                            inventoryItem1.setMeltingTime(
                                inventoryItem1.getMeltingTime()
                                    + 0.1F
                                        * (this.getHeat() / 100.0F)
                                        * (1.0F + (float)(this.getMeltingSkill(inventoryItem1) * 3) / 100.0F)
                                        * zombie.GameTime.instance.getMultiplier()
                            );
                        }

                        if (inventoryItem1.getMeltingTime() == 100.0F) {
                            inventoryItem0 = inventoryItem1;
                        }
                    } else {
                        inventoryItem1.setItemHeat(inventoryItem1.getItemHeat() - 0.001F * (this.getHeat() / 100.0F) * zombie.GameTime.instance.getMultiplier());
                        inventoryItem1.setMeltingTime(
                            inventoryItem1.getMeltingTime() - 0.1F * (this.getHeat() / 100.0F) * zombie.GameTime.instance.getMultiplier()
                        );
                    }
                }
            }

            if (inventoryItem0 != null) {
                if (inventoryItem0.getWorker() != null && !inventoryItem0.getWorker().isEmpty()) {
                    for (int int1 = 0; int1 < IsoPlayer.numPlayers; int1++) {
                        IsoPlayer isoPlayer = IsoPlayer.players[int1];
                        if (isoPlayer != null && !isoPlayer.isDead() && inventoryItem0.getWorker().equals(isoPlayer.getFullName())) {
                            break;
                        }
                    }
                }

                float float0 = inventoryItem0.getMetalValue()
                    + (inventoryItem0.getMetalValue() * (1.0F + (float)(this.getMeltingSkill(inventoryItem0) * 3) / 100.0F) - inventoryItem0.getMetalValue());
                if (drainableComboItem != null) {
                    if (float0 + drainableComboItem.getUsedDelta() / drainableComboItem.getUseDelta() > 1.0F / drainableComboItem.getUseDelta()) {
                        float0 -= 1.0F / drainableComboItem.getUseDelta() - drainableComboItem.getUsedDelta() / drainableComboItem.getUseDelta();
                        drainableComboItem.setUsedDelta(1.0F);
                        drainableComboItem = (DrainableComboItem)InventoryItemFactory.CreateItem("Base.IronIngot");
                        drainableComboItem.setUsedDelta(0.0F);
                        this.getContainer().addItem(drainableComboItem);
                    }
                } else {
                    drainableComboItem = (DrainableComboItem)InventoryItemFactory.CreateItem("Base.IronIngot");
                    drainableComboItem.setUsedDelta(0.0F);
                    this.getContainer().addItem(drainableComboItem);
                }

                float float1 = 0.0F;
                float float2 = float0;

                while (float1 < float2) {
                    if (drainableComboItem.getUsedDelta() + float0 * drainableComboItem.getUseDelta() <= 1.0F) {
                        drainableComboItem.setUsedDelta(drainableComboItem.getUsedDelta() + float0 * drainableComboItem.getUseDelta());
                        float1 += float0;
                    } else {
                        float0 -= 1.0F / drainableComboItem.getUseDelta();
                        float1 += 1.0F / drainableComboItem.getUseDelta();
                        drainableComboItem.setUsedDelta(1.0F);
                        drainableComboItem = (DrainableComboItem)InventoryItemFactory.CreateItem("Base.IronIngot");
                        drainableComboItem.setUsedDelta(0.0F);
                        this.getContainer().addItem(drainableComboItem);
                    }
                }

                this.getContainer().Remove(inventoryItem0);
            }
        }
    }

    private void updateHeat() {
        if (!this.isFireStarted()) {
            this.heat = this.heat - this.heatDecrease * zombie.GameTime.instance.getMultiplier();
        } else if (this.getFuelAmount() == 0.0F) {
            this.setFireStarted(false);
        } else {
            this.fuelAmount = this.fuelAmount - this.fuelDecrease * (0.2F + this.heatIncrease / 80.0F) * zombie.GameTime.instance.getMultiplier();
            if (this.getHeat() < 20.0F) {
                this.heat = this.heat + this.heatIncrease * zombie.GameTime.instance.getMultiplier();
            }

            this.heat = this.heat - this.heatDecrease * 0.05F * zombie.GameTime.instance.getMultiplier();
        }

        if (this.heat < 0.0F) {
            this.heat = 0.0F;
        }

        if (this.fuelAmount < 0.0F) {
            this.fuelAmount = 0.0F;
        }
    }

    public int getMeltingSkill(InventoryItem meltItem) {
        if (meltItem.getWorker() != null && !meltItem.getWorker().isEmpty()) {
            for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
                IsoPlayer isoPlayer = IsoPlayer.players[_int];
                if (isoPlayer != null && !isoPlayer.isDead() && meltItem.getWorker().equals(isoPlayer.getFullName())) {
                    return isoPlayer.getPerkLevel(PerkFactory.Perks.Melting);
                }
            }
        }

        return 0;
    }

    @Override
    public void load(ByteBuffer input, int WorldVersion, boolean IS_DEBUG_SAVE) throws IOException {
        super.load(input, WorldVersion, IS_DEBUG_SAVE);
        this.fireStarted = input.get() == 1;
        this.heat = input.getFloat();
        this.fuelAmount = input.getFloat();
    }

    @Override
    public void save(ByteBuffer output, boolean IS_DEBUG_SAVE) throws IOException {
        super.save(output, IS_DEBUG_SAVE);
        output.put((byte)(this.isFireStarted() ? 1 : 0));
        output.putFloat(this.getHeat());
        output.putFloat(this.getFuelAmount());
    }

    @Override
    public String getObjectName() {
        return "StoneFurnace";
    }

    public float getHeat() {
        return this.heat;
    }

    public void setHeat(float _heat) {
        if (_heat > 100.0F) {
            _heat = 100.0F;
        }

        if (_heat < 0.0F) {
            _heat = 0.0F;
        }

        this.heat = _heat;
    }

    public boolean isFireStarted() {
        return this.fireStarted;
    }

    public void updateLight() {
        if (this.fireStarted && this.LightSource == null) {
            this.LightSource = new IsoLightSource(this.square.getX(), this.square.getY(), this.square.getZ(), 0.61F, 0.165F, 0.0F, 7);
            IsoWorld.instance.CurrentCell.addLamppost(this.LightSource);
        } else if (this.LightSource != null) {
            IsoWorld.instance.CurrentCell.removeLamppost(this.LightSource);
            this.LightSource = null;
        }
    }

    public void setFireStarted(boolean _fireStarted) {
        this.fireStarted = _fireStarted;
        this.updateLight();
        this.syncFurnace();
    }

    public void syncFurnace() {
        if (GameServer.bServer) {
            GameServer.sendFuranceChange(this, null);
        } else if (GameClient.bClient) {
            GameClient.sendFurnaceChange(this);
        }
    }

    public float getFuelAmount() {
        return this.fuelAmount;
    }

    public void setFuelAmount(float amount) {
        if (amount > 100.0F) {
            amount = 100.0F;
        }

        if (amount < 0.0F) {
            amount = 0.0F;
        }

        this.fuelAmount = amount;
    }

    public void addFuel(float amount) {
        this.setFuelAmount(this.getFuelAmount() + amount);
    }

    @Override
    public void addToWorld() {
        IsoWorld.instance.getCell().addToProcessIsoObject(this);
    }

    @Override
    public void removeFromWorld() {
        if (this.emitter != null) {
            this.emitter.stopAll();
            IsoWorld.instance.returnOwnershipOfEmitter(this.emitter);
            this.emitter = null;
        }

        super.removeFromWorld();
    }

    public float getFuelDecrease() {
        return this.fuelDecrease;
    }

    public void setFuelDecrease(float _fuelDecrease) {
        this.fuelDecrease = _fuelDecrease;
    }

    public float getHeatDecrease() {
        return this.heatDecrease;
    }

    public void setHeatDecrease(float _heatDecrease) {
        this.heatDecrease = _heatDecrease;
    }

    public float getHeatIncrease() {
        return this.heatIncrease;
    }

    public void setHeatIncrease(float _heatIncrease) {
        this.heatIncrease = _heatIncrease;
    }
}
